<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="../../react18/react.development.js"></script>
    <script src="../../react18/react-dom.development.js"></script>
    <script src="../../react18/babel.min.js"></script>
  </head>
  <body>
    <div id="app"></div>
    <script type="text/babel">
      class Message extends React.Component {
        state = {
          opacity: 1,
          timer: null,
        };

        componentDidMount() {
          let { opacity } = this.state;
          var timer = setInterval(() => {
            opacity -= 0.08;
            if (opacity <= 0) {
              opacity = 1;
            }
            this.setState({
              opacity: opacity,
            });
          }, 100);
          this.setState({
            timer: timer,
          });
        }
        render() {
          return (
            <div>
              <h2 style={{ opacity: this.state.opacity }}>
                学习好难啊，我该怎么办？？？
              </h2>
            </div>
          );
        }
        componentWillUnmount() {
          const { timer } = this.state;
          console.log("马上就要销毁我了");
          console.log(timer, "********");
          clearInterval(timer);
        }
      }

      class App extends React.Component {
        state = {
          isStudyReact: true,
        };
        render() {
          const { isStudyReact } = this.state;
          return (
            <div>
              {isStudyReact ? <Message /> : ""}
              <button onClick={this.changeStudyState}>
                {isStudyReact ? "我不学了！！！" : "我还得学"}
              </button>
            </div>
          );
        }
        changeStudyState = () => {
          const { isStudyReact } = this.state;
          this.setState({
            isStudyReact: !isStudyReact,
          });
        };
      }
      ReactDOM.createRoot(document.getElementById("app")).render(<App />);
    </script>
  </body>
</html>
